package com.example.dangerouscargodriver.controller;

import android.content.Context;
import android.os.Message;
import android.util.Log;

import com.baiju.netmanager.Http.HttpRequest;
import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.netmanager.Http.INetResponse;
import com.example.dangerouscargodriver.DangerousCargoDriverApplication;
import com.example.dangerouscargodriver.entry.BankAccount;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.event.LoadHttpResourceStatus;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class BankAccountInfoController extends Controller{

    public static final int EVENT_BANK_ACCOUNT_LOADING = 0;
    public static final int EVENT_BANK_ACCOUNT_LOAD_DONE = 1;

    public static final int BANK_BIND_STATUS_NONE = 0;
    public static final int BANK_BIND_STATUS_DONE = 1;

    private static final int MSG_LOAD_BANK_ACCOUNT = 0;
    private static final int MSG_LOAD_BANK_ACCOUNT_DONE = 1;
    private static final  int MSG_ADD_LISTENER = 2;

    private static final String TAG = "BankAccountController";
    private static BankAccountInfoController mInstance = null;

    private int mLoadHttpStatus = EVENT_BANK_ACCOUNT_LOADING;
    private int mBindCardStatus = BANK_BIND_STATUS_NONE;

    private HttpRequest mHttp = null;
    private HttpResponseListener mHttpListener = null;

    private BankAccount mBankAccount = null;

    private HashMap<Class, IControllerEventListener> mControllerListener = null;

    public static BankAccountInfoController Instance(){
        if(mInstance == null){
            mInstance = new BankAccountInfoController(DangerousCargoDriverApplication.getAppContext());
        }

        return mInstance;
    }

    public BankAccountInfoController(Context context){
        mHttpListener = new HttpResponseListener();
        mControllerListener = new HashMap<>();
        mHttp = HttpRequest.Instance(context);
        mHttp.attachResponseListener(mHttpListener, BankAccountInfoController.class);
        mHttp.startUp();

        loadBankAccountInfo();
    }

    public boolean attachControllerListener(Class object, IControllerEventListener listener){
        if(mControllerListener.containsKey(object)){
            return false;
        }
        else {
            mControllerListener.put(object, listener);
            Message msg = new Message();
            msg.what = MSG_ADD_LISTENER;
            msg.obj = object;
            sendMessage(msg);

            return true;
        }
    }

    public boolean unAttachControllerListener(Class object, IControllerEventListener listener){
        if(mControllerListener.containsKey(object) && mControllerListener.get(object) == listener){
            mControllerListener.remove(object);
            return true;
        }
        else {
            return false;
        }
    }

    private void loadBankAccountInfo(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());
        header.put("app-type", "Android");

        mHttp.sendRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_USER_BANK_ACCOUNT,
                header,
                null,
                RemoteAPICmd.REQUEST_V1_USER_BANK_ACCOUNT,
                BankAccountInfoController.class);
    }

    public void updateBindingInfo(){
        loadBankAccountInfo();
    }

    public int getCardBindStatus(){
        return mBindCardStatus;
    }

    public BankAccount getBankCardInfo(){
        return mBankAccount;
    }

    @Override
    public void handleControllerMessage(Message msg){
        switch (msg.what){
            case MSG_LOAD_BANK_ACCOUNT_DONE:
                mLoadHttpStatus = EVENT_BANK_ACCOUNT_LOAD_DONE;
                for (java.lang.Class key : mControllerListener.keySet()) {
                    LoadHttpResourceStatus status = new LoadHttpResourceStatus();
                    status.setWhat(mLoadHttpStatus);
                    mControllerListener.get(key).onEvent(Event.EVENT_LOAD_HTTP_BANK_ACCOUNT, status.toJson());
                }
                break;
            case MSG_LOAD_BANK_ACCOUNT:
                loadBankAccountInfo();
                break;
            case MSG_ADD_LISTENER:
                LoadHttpResourceStatus status = new LoadHttpResourceStatus();
                status.setWhat(mLoadHttpStatus);
                mControllerListener.get(msg.obj).onEvent(Event.EVENT_LOAD_HTTP_BANK_ACCOUNT, status.toJson());
                break;
            default:
                break;
        }
    }

    private class HttpResponseListener implements INetResponse {

        @Override
        public void onResponse(int actionId, String response) {
            try {
                JSONObject res = new JSONObject(response);
                Gson gson = new Gson();
                if(actionId == RemoteAPICmd.REQUEST_V1_USER_BANK_ACCOUNT){
                    Log.d(TAG, "onResponse: " + response);
                    ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                    if(responseInfo != null && responseInfo.getStatus() == 1){
                        mBankAccount = gson.fromJson(res.getString("data"),
                                BankAccount.class);
                        mBindCardStatus = BANK_BIND_STATUS_DONE;
                    }
                    if(responseInfo != null && responseInfo.getStatus() == 0){
                        //用户未绑定银行卡
                        mBindCardStatus = BANK_BIND_STATUS_NONE;
                    }
                    else{
                        //用户未登录
                    }

                    Message msg = new Message();
                    msg.what = MSG_LOAD_BANK_ACCOUNT_DONE;
                    sendMessage(msg);
                }
                else{

                }
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onErrorResponse(int actionId, String response) {
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_BANK_ACCOUNT){
                Message message = new Message();
                message.what = MSG_LOAD_BANK_ACCOUNT;
                sendMessageDelayed(message, 1000);
            }
        }
    }
}
